/*
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package jmmos.gameserver.model.skills.mmoskills;

import jmmos.gameserver.model.MMOObject;
import jmmos.gameserver.model.StatsSet;
import jmmos.gameserver.model.actor.MMOCharacter;
import jmmos.gameserver.model.actor.instance.MMOPcInstance;
import jmmos.gameserver.model.skills.MMOSkill;
import jmmos.gameserver.network.SystemMessageId;
import jmmos.gameserver.network.serverpackets.PetItemList;
import jmmos.gameserver.network.serverpackets.SystemMessage;
import jmmos.util.Rnd;

/**
 * @author Nemesiss
 */
public class MMOSkillCreateItem extends MMOSkill
{
	private final int[] _createItemId;
	private final int _createItemCount;
	private final int _randomCount;
	
	public MMOSkillCreateItem(StatsSet set)
	{
		super(set);
		_createItemId = set.getIntegerArray("create_item_id", ";");
		_createItemCount = set.getInteger("create_item_count", 0);
		_randomCount = set.getInteger("random_count", 1);
	}
	
	@Override
	public void useSkill(MMOCharacter activeChar, MMOObject[] targets)
	{
		MMOPcInstance player = activeChar.getActingPlayer();
		if (activeChar.isAlikeDead())
		{
			return;
		}
		if (activeChar.isPlayable())
		{
			if ((_createItemId == null) || (_createItemCount == 0))
			{
				SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.S1_PREPARED_FOR_REUSE);
				sm.addSkillName(this);
				player.sendPacket(sm);
				return;
			}
			
			int count = _createItemCount + Rnd.nextInt(_randomCount);
			int rndid = Rnd.nextInt(_createItemId.length);
			if (activeChar.isPlayer())
			{
				player.addItem("Skill", _createItemId[rndid], count, activeChar, true);
			}
			else if (activeChar.isPet())
			{
				activeChar.getInventory().addItem("Skill", _createItemId[rndid], count, player, activeChar);
				player.sendPacket(new PetItemList(activeChar.getInventory().getItems()));
			}
		}
	}
}
